package com.dianjing.dianqun.service;
/**  
 *  @date  : 2017年11月7日
 *  
 *  @author: baoyongtao 
 *  
 *  @see :  
 *   
 */

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Transactional;

import com.dianjing.dianqun.common.base.CrudService;
import com.dianjing.dianqun.dao.ContentCommentExtMapper;
import com.dianjing.dianqun.dao.ContentCommentMapper;
import com.dianjing.dianqun.model.Content;
import com.dianjing.dianqun.model.ContentComment;
import com.dianjing.dianqun.model.ContentCommentExt;

/**
 * 文章评论
 * 
 * @author djn104
 *
 */
@Service
@Transactional(readOnly = true, isolation = Isolation.DEFAULT)
public class ContentCommentService extends CrudService<ContentCommentMapper, ContentComment> {

	@Autowired
	private ContentCommentMapper commentMapper;

	@Autowired
	private ContentCommentExtMapper commentExtMapper;
	
	/**
	 * 根据用户查询评论 自己可看到自己  可以为空
	 * @param contentId
	 * @param userId
	 * @return
	 */
	public List<ContentComment> selectByContentId(Integer contentId, Integer userId) {
		return commentMapper.selectByContentId(contentId,userId);
	}

	@Transactional(readOnly = false, isolation = Isolation.DEFAULT)
	public ContentComment save(Content content, Integer fromUserId, String comment) {
		ContentComment contentComment = new ContentComment();
		contentComment.setAuthorUserId(content.getUserId());
		contentComment.setCommentUserId(fromUserId);
		contentComment.setCreateTime(new Date());
		contentComment.setContentId(content.getContentId());
		contentComment.setIsChecked(0);
		contentComment.setSiteId(content.getSiteId());
		contentComment.setIsRecommend(false);
		commentMapper.insertSelective(contentComment);
		ContentCommentExt commentExt = new ContentCommentExt(contentComment.getCommentId(), null, comment, null);
		commentExtMapper.insertSelective(commentExt);
		contentComment.setCommentExt(commentExt);
		return contentComment;
	}

	/**
	 * 根据评论id查询评论 自己可以查看自己得评论 为null 忽略自己
	 * @param commentId
	 * @param userId
	 * @return
	 */
	public ContentComment selectByUserIdChecked(Integer commentId, Integer userId) {
		return commentMapper.selectByUserIdChecked(commentId,userId);
	}

}
